package com.link_with_health.utils;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

@Component("httpConnUtil")
public class HttpConnUtil {
    //private static final Logger logger = Logger.getLogger("httpConnUtil");

    private int num;
    private CloseableHttpClient httpClient;
    private HttpPost httpPost;
    private HttpGet httpGet;
    private String tokenName;
    private ResponseHandler<String> responseHandler;

    @PostConstruct
    private void init() {
        num = 0;
        httpClient = HttpClients.createDefault();
        httpPost = new HttpPost();
        httpGet = new HttpGet();
        tokenName = "access_token";
        responseHandler = new BasicResponseHandler();
        httpPost.addHeader("Content-Type","application/json;charset=UTF-8");
        httpPost.setHeader("Accept","application/json");
    }

    /**
     * 发送post请求，以json的格式
     * @param url url
     * @param body 请求体 ->以json形式的字符串
     * @param token 签权
     * @return 响应结果
     * @throws Exception
     */
    public String httpPost(String url, String body, String token)
            throws Exception{
        //设置url
        httpPost.setURI(URI.create(url));
        //设置请求头
        httpPost.removeHeaders(tokenName);
        if(token != null && !"".equals(token)){
            httpPost.addHeader(tokenName,token);
        }

        //设置请求体
        //System.out.println("body: "+body);
        httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8));
        //发送url请求,获取返回信息
        /*
         * 使用通过EntityUtils.toString给responseContent赋值
         * 会加大消耗,因为entity也已经接收信息了
         * CloseableHttpResponse response = httpClient.execute(httpPost);
         * HttpEntity entity = response.getEntity();
         * String responseContent = EntityUtils.toString(entity,"UTF-8");
         * */
        String responseContent = httpClient.execute(httpPost, responseHandler);
        //关闭连接
        /*
         * 使用execute(HttpRequest, ResponseHandler)会调用关闭流
         * response.close();
         * */
        //httpClient.close();
        //System.out.println("responseContent: "+responseContent);
        return responseContent;

    }

    public void setHeader(String header, String value){
        if(this.httpPost.containsHeader(header))
            return;
        this.httpPost.setHeader(header, value);
    }

    public HttpConnUtil() {
    }

    public  CloseableHttpClient getHttpClient() {
        return httpClient;
    }

    public void setHttpClient(CloseableHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    public HttpPost getHttpPost() {
        return httpPost;
    }

    public void setHttpPost(HttpPost httpPost) {
        this.httpPost = httpPost;
    }

    public String getTokenName() {
        return tokenName;
    }

    public void setTokenName(String tokenName) {
        this.tokenName = tokenName;
    }


}
